require 'Common/define'
require 'Logic/Config'
require 'Common/functions'

UICharactorInfoHeroEquip = {};
HelperFunc.SetUIEnv(UICharactorInfoHeroEquip)
local this = UICharactorInfoHeroEquip;

this.gameObject = nil
this.hasInit = false

this.btnMountBtn = nil
this.btnUnMountBtn = nil
this.btnShop = nil; --快捷购买
this.btnQuickSwitch = nil ;
this.hasAddRedDots = false;

this.currentHeroData = nil
this.equipItemDatas = {}
this.curWeaponData = nil
this.btn_OneKeyEquipBreak = nil
this.breakCtrl = nil
this.breakEquipList = nil
this.breakItems = nil
this.breakTitle = nil
this.breakTip = nil
this.breakClose = nil
this.breakCostData = {}
this.breakEquipData = {}
this.breakCancel = nil
this.breakConfirm = nil
this.breakAni = nil

function UICharactorInfoHeroEquip.Init(data, closeFunc, isShowOneHero)
    --todo 应该小写 暂时先大写
    currentHeroData = logicMgr.HeroManager.getHeroDatabyPartnerId(data, true);
    this.goFunc = closeFunc;
    this.isShowOneHero = isShowOneHero;
    local name, package = getUIName(PanelNames.UICharactorInfoHeroEquip);
    panelMgr:ShowUI(name, package, this.onCreate, Config.useLocalUI);
end

function UICharactorInfoHeroEquip.onCreate(obj)
    gameObject = obj;

    if not hasInit then
        hasInit = true;
        local objHeadPos = obj:GetChild("HeadPos")
        local objHandPos = obj:GetChild("HandPos")
        local objLegPos = obj:GetChild("LegPos")
        local objBodyPos = obj:GetChild("BodyPos")
        btnMountBtn = obj:GetChild("MountBtn")
        btnMountBtn.gameObjectName = "MountBtn";
        btnUnMountBtn = obj:GetChild("UnMountBtn")
        btnUnMountBtn.gameObjectName = "UnMountBtn";
        this.btnShop = obj:GetChild("ShopBtn");
        btnInlay = obj:GetChild("inlaylink")
        inlayController = btnInlay:GetController("c1")
        lockText = btnInlay:GetChild("unlock");
        inlayItem = btnInlay:GetChild("item")
        this.unlockTime = btnInlay:GetChild("unlockTime")
        btnInlay:GetChild("unlock2").text = getLanguage("UICharactorChipButton3")
        btnInlay:GetChild("unlock3").text = getLanguage("UICharactorChipButton2")
        this.effect_zwxq = btnInlay:GetChild("effect_zwxq")
        this.btn_OneKeyEquipBreak = obj:GetChild("btnQuickBreak")
        this.btnSource = obj:GetChild("btnSource")
        this.sourceUIobj = obj:GetChild("UISourceMultiple");
        this.showSourceController = obj:GetController("showSource")
        this.btnQuick = obj:GetChild("btnQuick")
        this.btnQuick.title = getLanguage("EquipRefine_QuickRefineBtn")
        this.ctrlLevelUp = obj:GetController("levelUp")
        this.ctrlLevelUp.selectedIndex = 1
        btnQuickSwitch = obj:GetChild("btnQuickSwitch")
        this.breakAni = obj:GetChild("effect_zbjl").wrapTarget
        this.isShowOneController = obj:GetController("isShowOne")
        this.breakCtrl = obj:GetController("showBreakResult")
        local breakCom = obj:GetChild("UIBreakConfirm")
        this.breakEquipList = breakCom:GetChild("list")
        this.breakItems = breakCom:GetChild("listCost")
        this.breakTitle = breakCom:GetChild("title")
        this.breakTip = breakCom:GetChild("title2")
        this.breakClose = breakCom:GetChild("BgClose")
        this.breakCancel = breakCom:GetChild("CancelBtn")
        this.breakConfirm = breakCom:GetChild("ConfirmBtn")
        this.createEquipItem(EnumConst.EquipPartEnum.HAND, objHandPos);
        this.createEquipItem(EnumConst.EquipPartEnum.HEAD, objHeadPos);
        this.createEquipItem(EnumConst.EquipPartEnum.BODY, objBodyPos);
        this.createEquipItem(EnumConst.EquipPartEnum.LEG, objLegPos);
        btnInlay.text = ""
        this.addEvent();
    end
    if this.isShowOneHero then
        this.isShowOneController.selectedIndex = 1;
    else
        this.isShowOneController.selectedIndex = 0;
    end
    this.refresh();
    this.refreshShop();
end

function UICharactorInfoHeroEquip.addEvent()
    btnQuickSwitch.onClick:Add(this.QuickSwitch)
    btnMountBtn.onClick:Add(this.onClickMount)
    btnUnMountBtn.onClick:Add(this.onClickUnmount)
    this.btnSource.onClick:Add(this.onClickSource)
    btnInlay.onClick:Add(this.onClickInlay)
    this.btnQuick.onClick:Add(this.ClickAllRefine)
    this.btnShop.onClick:Add(this.onClickShop);
    this.btn_OneKeyEquipBreak.onClick:Add(this.QuickEquipBreak)
    this.breakClose.onClick:Add(function()
        this.breakCtrl.selectedIndex = 0
        logicMgr.EquipManager.tempBreakData = {}
    end)
end

function UICharactorInfoHeroEquip.FreshQuickEquipBreak(msg)
    Util.PlayAction(this.breakAni,"idle",false)
    for _,v in pairs(msg.partnerEquips.items) do
        logicMgr.EquipManager.delItemData(v.itemId)
        logicMgr.EquipManager.addItemData(v,-1,"",currentHeroData.partnerId);
    end
    logicMgr.HeroManager.updateHeroBaseAttr(msg.partnerId,true,nil,nil,nil,true)
    this.reGetData()
end

function UICharactorInfoHeroEquip.IsHaveTheItem(itemId)
    for k,v in ipairs(this.breakCostData) do
        if v["itemModelId"] == itemId then
            return k
        end
    end
    return nil
end

function UICharactorInfoHeroEquip.BreakEquipRender(idx,obj)
    local cfg = this.breakEquipData[idx+1]
    local ctrItem = CtrlManager.GetCtrl(PanelNames.EquipItem);
    local equips = logicMgr.EquipManager.getHeroEquipList(currentHeroData.partnerId)
    ctrItem.initData(obj,cfg.equip, cfg.equip and HeroManager.GetSuit(equips, cfg.equip) or 0)
end

function UICharactorInfoHeroEquip.BreakItemRender(idx,obj)
    local cfg = this.breakCostData[idx+1]
    local itemCtrl =  CtrlManager.GetCtrl(PanelNames.Item);
    local data = {}
    data["itemModelId"] = cfg.itemModelId
    data["num"] = cfg.num
    itemCtrl.initData(obj,data)
end

function UICharactorInfoHeroEquip.QuickEquipBreak()
    local isCan = nil
    local data = {}
    local indexs = {2,1,4,3}
    for _, v in ipairs(indexs) do
        local equipData = equipItemDatas[v].equip
        if equipData and equipData.upTargetId and equipData.upTargetId ~= 0 then
            local tempData = logicMgr.EquipManager.PreviewBreak(equipData.itemModelId)
            if table.getn(tempData) > 0 then
                data[equipData.part] = tempData
            end
            if (not isCan) and table.getn(data) > 0 then
                isCan = true
            end
        end
    end
    if isCan then
        this.breakCtrl.selectedIndex = 1
        local temp = {}
        for _,v in pairs(equipItemDatas) do
            temp = clone(v)
            break
        end
        this.breakCostData = {}
        this.breakEquipData = {}
        for part,v in pairs(data) do
            for k,val in pairs(v["items"]) do
                local index = this.IsHaveTheItem(k)
                if index then
                    this.breakCostData[index].num = this.breakCostData[index].num + val
                else
                    table.insert(this.breakCostData,{["itemModelId"]=k,["num"] = val})
                end
            end
            local cfg = dataTable.getDataTableByStringId("t_equip", v["targetId"])
            local temp = clone(equipItemDatas[part])
            temp.equip["quality"] = cfg.f_EquipQuality
            table.insert(this.breakEquipData,temp)
        end
        this.breakItems.itemRenderer = this.BreakItemRender
        this.breakItems.numItems = #this.breakCostData
        this.breakEquipList.itemRenderer = this.BreakEquipRender
        this.breakEquipList.numItems = #this.breakEquipData
        this.breakCancel.onClick:Clear()
        this.breakCancel.onClick:Add(function()
            logicMgr.EquipManager.tempBreakData = {}
            this.breakCtrl.selectedIndex = 0
        end)
        this.breakConfirm.onClick:Clear()
        this.breakConfirm.onClick:Add(function()
            this.breakCtrl.selectedIndex = 0
            logicMgr.EquipManager.tempBreakData = {}
            equipHandler.SendQuickEquipBreak(currentHeroData.partnerId)
        end)
    else
        UISysTips.AddMsg(getLanguage("EquipBreak_QuickBreak"))
    end
end

function UICharactorInfoHeroEquip.QuickSwitch()
    local pid = this.currentHeroData.partnerId
    local equips = logicMgr.EquipManager.getHeroEquipList(pid)
    local hasSuit
    local isAllSameSuit = true
    local suitCount = 0
    local lastMinQCfg = nil
    for k, _equipItem in pairs(equips) do
        local minQ = this.currentHeroData.quality
        local minQCfg = dataTable.getDataTableByMultiId("t_equipsuit",_equipItem.suit,_equipItem.heroType,minQ)

        if minQCfg then
            if minQCfg.f_EquipSuit >= 1 then
                hasSuit = true
                suitCount = suitCount+1
            end

            if lastMinQCfg then
                if lastMinQCfg.f_EquipSuit ~= minQCfg.f_EquipSuit then
                    isAllSameSuit = false
                end
            end

            if lastMinQCfg == nil then
                lastMinQCfg = minQCfg
            end
        else
        end
    end
    if hasSuit then
        if isAllSameSuit or suitCount ==1 then
            if this.last_click_switch_suit ~= nil and UnityEngineTime.realtimeSinceStartup - this.last_click_switch_suit < 5 then
                UISysTips.AddMsg(getLanguage("EquipSwitchTips1"))
                return
            end
            this.last_click_switch_suit = logicMgr.EquipManager.getLastSendSwitchTime()
            equipHandler.sendSwitchSuit(pid,-1,-1)
        else
            showUI(PanelNames.UIEquipSwitchSuit,pid,isAllSameSuit)
        end
    end
end

function UICharactorInfoHeroEquip.removeEvent()
    btnMountBtn.onClick:Clear()
    btnUnMountBtn.onClick:Clear()
    this.btnSource.onClick:Clear()
    btnInlay.onClick:Clear()
    this.btnQuick.onClick:Clear()
    this.btnShop.onClick:Clear();
    this.btn_OneKeyEquipBreak.onClick:Clear()
    this.breakClose.onClick:Clear()
end

function UICharactorInfoHeroEquip.LocalListenEvent()
    ListenEvent(Config.EventType.Fresh_Equip, this.reGetData);
    ListenEvent(Config.EventType.Fresh_Refine, this.reGetData);
    ListenEvent(Config.EventType.QuickEquipBreak,this.FreshQuickEquipBreak)
    ListenEvent(Config.EventType.Fresh_Bag, this.updateSource);
    ListenEvent(Config.EventType.Fresh_BagUseItemBack, logicMgr.ItemManager.showGetItem);
    ListenEvent(Config.EventType.Fragment_Wear, this.RefreshDebris)
    ListenEvent(Config.EventType.Fragment_Unwear, this.RefreshDebris)
    ListenEvent(Config.EventType.Fragment_Replace, this.RefreshDebris)
    ListenEvent(Config.EventType.DebrisUpdate, this.RefreshDebris)
end

function UICharactorInfoHeroEquip.LocalCloseEvent()
    CloseEvent(Config.EventType.Fresh_Equip, this.reGetData);
    CloseEvent(Config.EventType.Fresh_Refine, this.reGetData);
    CloseEvent(Config.EventType.QuickEquipBreak,this.FreshQuickEquipBreak)
    CloseEvent(Config.EventType.Fresh_Bag, this.updateSource);
    CloseEvent(Config.EventType.Fresh_BagUseItemBack, logicMgr.ItemManager.showGetItem);
    CloseEvent(Config.EventType.Fragment_Wear, this.RefreshDebris)
    CloseEvent(Config.EventType.Fragment_Unwear, this.RefreshDebris)
    CloseEvent(Config.EventType.Fragment_Replace, this.RefreshDebris)
    CloseEvent(Config.EventType.DebrisUpdate, this.RefreshDebris)
    logicMgr.FuncOpenManager.removeStatusCtr(PanelNames.UICharactorInfoHeroEquip);
end

function UICharactorInfoHeroEquip.OnDestroy()
    UIRedDots.removeViewEvent(PanelNames.UICharactorInfoHeroEquip)
    this.removeEvent();
    if equipHandler.refreshHeroList then
        equipHandler.refreshHeroList = nil
        if not logicMgr.GuideMgr.IsGuiding() then --与引导排序冲突
            FireEvent(Config.EventType.ReFreshHeroPower)
        end
    end
    utimer.remove(this.clickRefineTimerId)
    this.clickRefineTimerId = nil
end

function UICharactorInfoHeroEquip.OnEnable()
    this.LocalListenEvent();
end

function UICharactorInfoHeroEquip.OnDisable()
    this.LocalCloseEvent();
end

function UICharactorInfoHeroEquip.reGetData()
    if currentHeroData ~= nil then
        currentHeroData = logicMgr.HeroManager.getHeroDatabyPartnerId(tostring(currentHeroData["partnerId"]), true);
        this.refresh();
    end
end

function UICharactorInfoHeroEquip.refresh()
    
    local equips = logicMgr.EquipManager.getHeroEquipList(currentHeroData.partnerId)
    local ctrItem = CtrlManager.GetCtrl(PanelNames.EquipItem); 
    for _, v in pairs(equipItemDatas) do
        if v.part ~= EnumConst.EquipPartEnum.WEAPON then
            -- if ctrItem then
            --     ctrItem.initData(v.obj, nil, 0);
            -- end
            v.equip = nil
            for _, v1 in pairs(equips) do
                if v1.part == v.part then 
                    v.equip = v1
                    break
                end
            end 
            if ctrItem and not HelperFunc.IsNull(v.obj) then
                ctrItem.initData(v.obj, v.equip,  v.equip and HeroManager.GetSuit(equips, v.equip) or 0)
                UIRedDots.addViewListen(PanelNames.UICharactorInfoHeroEquip, gameObject.gameObjectName .. "MountBtn", btnMountBtn, Config.RedDotsType.Allwear, currentHeroData.partnerId);
                if v.equip ~= nil then
                    --UIRedDots.addViewListen(PanelNames.UICharactorInfoHeroEquip, gameObject.gameObjectName .. v.part, v.obj, Config.RedDotsType.Allrefine, nil, v.equip);
                    UIRedDots.addViewListen(PanelNames.UICharactorInfoHeroEquip, gameObject.gameObjectName .. v.part, v.obj, Config.RedDotsType.EquipBreak, nil, v.equip);
                else
                    UIRedDots.removeListen(gameObject.gameObjectName .. v.part, Config.RedDotsType.Allrefine);
                    UIRedDots.removeListen(gameObject.gameObjectName .. v.part, Config.RedDotsType.EquipBreak);
                end
            end
        end
    end
    UIRedDots.addViewListen(PanelNames.UICharactorInfoHeroEquip, this.btn_OneKeyEquipBreak.name, this.btn_OneKeyEquipBreak,Config.RedDotsType.EquipBreak, currentHeroData.partnerId);
    UIRedDots.addViewListen(PanelNames.UICharactorInfoHeroEquip, this.btnQuick.name, this.btnQuick, Config.RedDotsType.OneKeyRefine, currentHeroData.partnerId);
    if logicMgr.HeroManager.IsHeroByModelId(currentHeroData.partnerModelId) then
        FuncOpenManager.addStatusCtr(PanelNames.UICharactorInfoHeroEquip, Config.FuncOpenType.WeaponRefine, btnInlay.name, btnInlay, { heroId = currentHeroData.partnerId });
    end
    UICharactorInfoHeroEquip.RefreshDebris()
    FuncOpenManager.addStatusCtr(PanelNames.UICharactorInfoHeroEquip, Config.FuncOpenType.allEquipRefine, this.btnQuick.name, this.btnQuick);

    local suitCount = 0

    for k, _equipItem in pairs(equips) do
        local minQ = this.currentHeroData.quality
        local minQCfg = dataTable.getDataTableByMultiId("t_equipsuit",_equipItem.suit,_equipItem.heroType,minQ)
        if minQCfg then
            if minQCfg.f_EquipSuit >= 1 then

                suitCount = suitCount+1
            end
        end
    end
    local showSwitchBtn = suitCount >=1
    btnQuickSwitch.visible = showSwitchBtn
end

--奇异碎片
function UICharactorInfoHeroEquip.RefreshDebris()
    this.curWeaponData = logicMgr.EquipManager.tabWeapon[tostring(this.currentHeroData.partnerId)];

    local level = G.dataTable["t_global"]["53"]["f_int_value"];
    local funcConfig = G.dataTable["t_functionswitch"];
    local singleConfig = funcConfig[tostring(Config.FuncOpenType.WeaponRefine)]

    local open = logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.WeaponRefine, false, nil, { heroId = currentHeroData.partnerId })

    if logicMgr.FuncOpenManager.unlock_check5(singleConfig, currentHeroData.partnerId) then
        local itemId = StrangeFragmentManager.GetCurrentEquipFragment(this.currentHeroData.partnerId)
        if itemId then
            local item = CtrlManager.GetCtrl(PanelNames.Item);
            if item ~= nil then
                -- item.initData(inlayItem, itemId, 1, true, nil, this.currentHeroData.partnerId);
                StrangeFragmentManager.SetItemData(inlayItem, itemId, false)
                inlayItem.onClick:Clear()
                inlayItem.onClick:Add(UICharactorInfoHeroEquip.onClickInlay)

                if open and (StrangeFragmentManager.CheckTheChipChangeCondition(itemId) or StrangeFragmentManager.CheckTheChipBroken(itemId)) then --StrangeFragmentManager.CheckTheChipUpgrade(itemId) or
                    btnInlay:GetChild("UIRedDots"):GetController("c1").selectedIndex = 2
                else
                    btnInlay:GetChild("UIRedDots"):GetController("c1").selectedIndex = 0
                end
            end
            inlayController.selectedIndex = 0;
        else
            if this.currentHeroData.level > 1 then
                if open and (StrangeFragmentManager.CheckTheChipChangeCondition(itemId) or StrangeFragmentManager.CheckTheChipBroken(itemId)) then
                    btnInlay:GetChild("UIRedDots"):GetController("c1").selectedIndex = 2
                else
                    btnInlay:GetChild("UIRedDots"):GetController("c1").selectedIndex = 0
                end
            end
            
            inlayController.selectedIndex = 2
        end
    else
        inlayController.selectedIndex = 1;
    end

    local _flag, openTime = logicMgr.FuncOpenManager.unlock_check6(singleConfig);
    if not _flag then
        this.unlockTime.text = string.format(getLanguage("UICharactorChipButton1"), math.ceil(openTime / (3600 * 24)));
        if inlayController.selectedIndex == 1 then
            inlayController.selectedIndex = 4;
        else
            inlayController.selectedIndex = 3
        end
    end


    local _fragmentSkillDesc = ""
    local _skillInfo, _skillDesc
    local _fragmentList = StrangeFragmentManager.GetFragmentInfoByPartnerId(this.currentHeroData.partnerId)
    if _fragmentList then
        inlayController.selectedIndex = 0
        _skillInfo = G.dataTable["t_skill"][tostring(_fragmentList.f_SkillId)]
        if _skillInfo and _skillInfo.f_Desc then
            _skillDesc = getLanguage(_skillInfo.f_Desc)
        else
            _skillDesc = ""
        end
        _fragmentSkillDesc = _skillDesc
        if this.effect_zwxq and this.effect_zwxq.wrapTarget then
            this.effect_zwxq.wrapTarget:SetActive(true)
            Util.PlayAction(this.effect_zwxq.wrapTarget, "idle2", true)
        end
    else
        -- this.fragmentEffectWrap:SetActive(false) 
        if this.effect_zwxq and this.effect_zwxq.wrapTarget  then
            this.effect_zwxq.wrapTarget:SetActive(false)
        end
    end
    btnInlay:GetChild("text").text = _fragmentSkillDesc
end

function UICharactorInfoHeroEquip.onClickInlay()
    -- 镶嵌
    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.WeaponRefine,true, btnInlay, { heroId = currentHeroData.partnerId }) then
        if inlayController.selectedIndex == 1 then
            --锁住 专武需达到10级才可解锁奇异碎片
            --UISysTips.AddMsg(getLanguage("UIWeapon_FragmentLimit"))
        else
            local _heroInfo = HeroManager.getHeroDatabyPartnerId(this.currentHeroData.partnerId, true)
            local _gemInfo = StrangeFragmentManager.GetCurrentEquipFragment(this.currentHeroData.partnerId)
            if _gemInfo then
                showUI(PanelNames.UIDebrisInfo, _gemInfo)
            else
                showUI(PanelNames.UIDebrisList, {heroId = this.currentHeroData.partnerId, gemInfo = _gemInfo}, this.goFunc)
            end
        end
    else
        --[[local funcConfig = G.dataTable["t_functionswitch"];
        local singleConfig = funcConfig[tostring(Config.FuncOpenType.WeaponRefine)];
        UISysTips.AddMsg(string.format(getLanguage("FunctionTipsDebris"), EnumConst.StarType[singleConfig.f_UnlockHeroRank / 10]));--]]
    end
end
function UICharactorInfoHeroEquip.DelayEnableClickAllRefine()
    this.isClickAllRefine = nil
    this.clickRefineTimerId = nil
end
--一键精炼
function UICharactorInfoHeroEquip.ClickAllRefine()
    if this.isClickAllRefine then
        UISysTips.AddMsg(getLanguage("Sys_Friend_Tips2"))
        return
    end
    this.isClickAllRefine = true
    this.clickRefineTimerId = utimer.delayCall(this.DelayEnableClickAllRefine,1000)
    local flag = logicMgr.EquipManager.CheckOneKeyRefine(this.currentHeroData.partnerId,true)
    if flag then
        local itemList = logicMgr.ItemManager.GetRefineMatList(false);
        local mapCfg = logicMgr.MapManager.getMapcfg()
        logicMgr.EquipManager.getEquipListByMinQuality(itemList, mapCfg.f_QuicklyRefineQuality)

        local tips = false
        for k, v in pairs(itemList) do
            local exp = 0
            if logicMgr.ItemManager.IsEquip(v.itemModelId) or v.itemType == EnumConst.ItemTableEnum.EquipBroken then
                if v.quality >= 8 then
                    tips = true
                    break
                end
            end
        end

        if tips then
            local pop = CtrlManager.GetCtrl(PanelNames.UICommonPop);
            pop.Init("128", function(b)
                if b then
                    equipHandler.ReqRefineOneClick(this.currentHeroData.partnerId)
                end
            end)
        else
            equipHandler.ReqRefineOneClick(this.currentHeroData.partnerId)
        end
    end
end
function UICharactorInfoHeroEquip.createEquipItem(equipType, obj)
    local data = {};
    data["obj"] = obj;
    data["part"] = equipType;
    obj.onClick:Clear()
    obj.onClick:Add(function()
        this.onClickEquip(obj, data)
    end)
    equipItemDatas[equipType] = data;
end

function UICharactorInfoHeroEquip.onClickMount()
    equipHandler.refreshHeroList = true
    if this.Mount() then
        --更换装备
        local _heroType = G.dataTable["t_hero"][tostring(currentHeroData.partnerModelId)]["f_HeroType"]
        if _heroType then
            AudioManager.TryPlaySoundByWear(_heroType, true)
        end
        AudioManager.PlayInteractionVoice(currentHeroData.partnerModelId, 0, AudioManager.InteractionTagTable[11].Tag)
    else
        logicMgr.GuideMgr.WaitingMsgDone()
    end
end

function UICharactorInfoHeroEquip.Mount()
    local flag = nil 
    local data = {}
    for k, v in pairs(equipItemDatas) do
        local compareEquip = logicMgr.EquipManager.getBestEquipbyPart(v.equip,v.part, currentHeroData["type"])
        if compareEquip ~= nil then
            flag = true
            data[v.part] = compareEquip.itemId 
        else
            data[v.part] = v.equip and v.equip.itemId or 0
        end
    end
    if flag then 
        equipHandler.sendOneKeyWearOrUnWear(this.currentHeroData.partnerId, data )
        printTable(data)
        return true
    else
        return false
    end

end

function UICharactorInfoHeroEquip.onClickUnmount()
    equipHandler.refreshHeroList = true 
    if this.Unmount() then
        local _heroType = G.dataTable["t_hero"][tostring(currentHeroData.partnerModelId)]["f_HeroType"]
        if _heroType then
            AudioManager.TryPlaySoundByWear(_heroType, false)
        end
    end
end

function UICharactorInfoHeroEquip.Unmount()
    local flag = nil
    local data = {}
    for k, v in pairs(equipItemDatas) do
        if v.equip ~= nil then
            flag = true
        end
        data[v.part] = 0
    end
    if flag then 
        equipHandler.sendOneKeyWearOrUnWear(currentHeroData.partnerId, data )
        return true
    else 
        return false
    end
end

function UICharactorInfoHeroEquip.onClickEquip(go, data)
    if data.part == EnumConst.EquipPartEnum.WEAPON then
        showUI(PanelNames.UIWeapon, currentHeroData.partnerId);
        return
    end
    local equipInfo = data.equip
    if equipInfo ~= nil then
        showUI(PanelNames.UIEquipInfo, currentHeroData, equipInfo,0);
    else
        equipInfo = {};
        equipInfo["part"] = data.part
        showUI(PanelNames.UIEquipList, currentHeroData, equipInfo,0);
    end
end

function UICharactorInfoHeroEquip.onClickSource() 
    this.showSourceController.selectedIndex = 1;
    this.showSourceList = { 200426 }
    this.updateSource();
end

function UICharactorInfoHeroEquip.refreshShop()
    this.btnShop.visible = false;
    local actState = logicMgr.CostManager.getFastConsumption();
    if actState == 0 or (actState == 1 and logicMgr.CostManager.isFastConsumptionItems(2)) then
        if not this.hasAddRedDots and actState == 0 then
            this.hasAddRedDots = true;
            UIRedDots.addViewListen(PanelNames.UICharactorInfoHeroEquip, this.btnShop.name, this.btnShop, Config.RedDotsType.CostGiftLink);
        end
        this.btnShop.visible = true;
    end
end

function UICharactorInfoHeroEquip.onClickShop()
    showUI(PanelResNames.UIGiftLink, 2, -1, function()
        this.RefreshDebris();
        FireEvent(Config.EventType.Fresh_refine);
    end);
end

function UICharactorInfoHeroEquip.updateSource()
    logicMgr.ItemManager.updateSourceUI(this.sourceUIobj, this.showSourceList, function()
        this.showSourceController.selectedIndex = 0;
    end, function()
        this.showSourceController.selectedIndex = 0;
        if this.goFunc then
            this.goFunc();
        end
    end);
end

return UICharactorInfoHeroEquip;
